app.controller('RecipientCtrl', function ($scope, ajax, $timeout, $modal, toaster, $state, $stateParams) {

	$scope.type = $stateParams.type;

	$scope.ids = [];

	$scope.statusList = [
		{ id: 1, name: "待付款" },
		{ id: 2, name: "支付中" },
		{ id: 3, name: "已支付" },
		{ id: 4, name: "已完成" },
		{ id: 5, name: "已取消" },
		{ id: 6, name: "订单超时" },
		{ id: 7, name: "退款中" },
		{ id: 8, name: "退款完成" },
		{ id: 9, name: "退款失败" }
	];

	$scope.checkbox = function () {
		var oneches = document.getElementsByName("checks");
		var ids = $scope.ids;
		for (var i = 0; i < oneches.length; i++) {
			var one = parseInt(oneches[i].value);
			if (ids.indexOf(one) != -1) {
				oneches[i].checked = true;
			}
		}
	};

	$scope.ids = [];
	$scope.checks = false;//默认未选中
	$scope.ckAll = false;//默认未选中
	$scope.choseArr = [];
	$timeout(function () {
		$scope.checkbox();
	}, 500);


	if ($scope.type == 1) {
		$scope.reload = $scope.page.loader('order/bk/{pageIndex}/{pageSize}/page');
	} else if ($scope.type == 2) {
		$scope.reload = $scope.page.loader('order/rt/{pageIndex}/{pageSize}/page');
	} else if ($scope.type == 3) {
		$scope.reload = $scope.page.loader('order/bs/{pageIndex}/{pageSize}/page');
	}

	$scope.$on('pageIndex', function (event, p) {
		$scope.reload(p, $scope.queryMap);
		$scope.checks = false;//默认未选中
		$scope.ckAll = false;//默认未选中
		$timeout(function () {
			$scope.checkbox();
		}, 500);
	});
	$scope.reload(1);

	//模糊查询
	$scope.queryMap = {};
	$scope.timeRange = {};
	$scope.timeDeparture = {};
	$scope.query = function (page) {
		if ($scope.statusList.selected != undefined) {
			$scope.queryMap.status = $scope.statusList.selected.id;
		} else if ($scope.statusList.selected == undefined) {
			delete $scope.queryMap.status;
		}

		if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
			var starTime = $scope.timeRange.startTime.pattern("yyyy-MM-dd");
			var endTime = $scope.timeRange.endTime.pattern("yyyy-MM-dd");
			$scope.queryMap.createTime = starTime + "#@" + endTime;
		} else if ($scope.timeRange.startTime == null || $scope.timeRange.endTime == null) {
			delete $scope.queryMap.createTime;
		}
		$scope.reload(page || 1, $scope.queryMap);
		$scope.checks = false;//默认未选中
		$scope.ckAll = false;//默认未选中
		$timeout(function () {
			$scope.checkbox();
		}, 500);
	};

	$scope.orderList = [];

	/*单选*/
	$scope.check = function (val) {
		if ($scope.ids.indexOf(val.id) < 0) {
			$scope.ids.push(val.id);
			$scope.orderList.push(val);
		} else {
			$scope.ids.splice($scope.ids.indexOf(val.id), 1);
			$scope.orderList.splice($scope.orderList.indexOf(val), 1);
		}
	};

	/*全选*/
	$scope.checkAll = function (c, v) {
		if (c == true) {
			$scope.checks = true;
			$scope.choseArr = v;
			//添加所选Id
			for (var i = 0; i < $scope.choseArr.length; i++) {
				if ($scope.ids.indexOf($scope.choseArr[i].id) >= 0) {
					continue;
				} else {
					$scope.ids.push($scope.choseArr[i].id);
					$scope.orderList.push($scope.choseArr[i]);
				}
			}
		} else {
			$scope.checks = false;
			for (var i = 0; i < $scope.choseArr.length; i++) {
				$scope.ids.splice($scope.ids.indexOf($scope.choseArr[i].id), 1);
				$scope.orderList.splice($scope.orderList.indexOf($scope.choseArr[i]), 1);
			}
		}
	};

	$scope.pasList = [];

	var inserter = new Req.POST({ resource: $scope.resource });

	$scope.send = function () {
		for (var i = 0; i < $scope.orderList.length; i++) {
			if ($scope.pasList.indexOf($scope.orderList[i].passengerId) >= 0) {
				continue;
			} else {
				$scope.pasList.push($scope.orderList[i].passengerId);
			}
		}
		if ($scope.pasList.length == 0) {
			toaster.pop('warning', '提示', '请至少勾选一个收件人！');
			return;
		}

		var modalInstance = $modal.open({
			templateUrl: 'template/system/notify/send.html?v=249bf12ad1',
			controller: 'NotifysSendCtrl',
			size: 'lg',
			resolve: {
				param: function () {
					return {
						"type": $scope.type,
						"length": $scope.pasList.length,
						"ids": $scope.ids.length
					};
				}
			}
		});

		modalInstance.result.then(function (param) {
			if ($scope.type == 1) {
				inserter.datalist = $scope.pasList;
				inserter.fields = { "subject": param.subject, "content": param.content };
				inserter.resource = 'notify/' + $scope.loginUser.id + '/bk';
				ajax.call(inserter).then(function (result) {
					if (result.code == 200) {
						toaster.pop('success', '成功', '发送消息成功。');
					} else {
						toaster.pop('error', '错误', '发送消息失败！');
					}
				}, function () { toaster.pop('error', '错误', '发送消息失败！'); });
			} else if ($scope.type == 2) {
				inserter.datalist = $scope.pasList;
				inserter.fields = { "subject": param.subject, "content": param.content };
				inserter.resource = 'notify/' + $scope.loginUser.id + '/rent';
				ajax.call(inserter).then(function (result) {
					if (result.code == 200) {
						toaster.pop('success', '成功', '发送消息成功。');
					} else {
						toaster.pop('error', '错误', '发送消息失败！');
					}
				}, function () { toaster.pop('error', '错误', '发送消息失败！'); });
			} else if ($scope.type == 3) {
				inserter.datalist = $scope.pasList;
				inserter.fields = { "subject": param.subject, "content": param.content };
				inserter.resource = 'notify/' + $scope.loginUser.id + '/bs';
				ajax.call(inserter).then(function (result) {
					if (result.code == 200) {
						toaster.pop('success', '成功', '发送消息成功。');
					} else {
						toaster.pop('error', '错误', '发送消息失败！');
					}
				}, function () { toaster.pop('error', '错误', '发送消息失败！'); });
			}

		});

	};

});